package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.core.entity.BaseDO;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpArchivesDAO;
import org.jsola.hr.dto.EmpArchivesAddDTO;
import org.jsola.hr.dto.EmpArchivesDTO;
import org.jsola.hr.dto.EmpArchivesUpdateDTO;
import org.jsola.hr.entity.EmpArchivesDO;
import org.jsola.hr.query.EmpArchivesQuery;
import org.jsola.hr.service.IEmpArchivesService;
import org.jsola.hr.vo.EmpArchivesListVO;
import org.jsola.hr.vo.EmpArchivesVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工档案信息
 *
 * @author zhr
 */
@Service("hrEmpArchivesServiceImpl")
public class EmpArchivesServiceImpl implements IEmpArchivesService {

    @Autowired
    private IEmpArchivesDAO empArchivesDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpArchivesVO save(EmpArchivesAddDTO empArchivesAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpArchivesDO empArchivesDO = empArchivesAddDTO.to(EmpArchivesDO.class);

        // 校验员工和公司
        checkEmpAndCompany(empArchivesDO.getEmpId(), empArchivesDO.getCompanyId(), tokenUser.getSiteId());

        // 保存
        empArchivesDO = save(empArchivesDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empArchivesDO.to(EmpArchivesVO.class);
    }

    /**
     * 校验员工和公司
     *
     * @param empId     员工id
     * @param companyId 公司id
     * @param siteId    站点
     */
    private void checkEmpAndCompany(String empId, String companyId, String siteId) {
        ParamException.hasLength(empId, "员工id不能为空");
        ParamException.hasLength(companyId, "公司id不能为空");
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpArchivesUpdateDTO empArchivesUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpArchivesDO empArchivesDO = empArchivesUpdateDTO.to(EmpArchivesDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empArchivesDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empArchivesIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empArchivesIds);
    }


    @Override
    public EmpArchivesVO selectById(String empArchivesId, String siteId) {
        EmpArchivesDO empArchivesDO = selectDOById(empArchivesId, siteId);
        if (empArchivesDO == null) {
            return null;
        }
        return empArchivesDO.to(EmpArchivesVO.class);
    }

    @Override
    public List<EmpArchivesListVO> select(EmpArchivesQuery empArchivesQuery, String siteId) {
        List<EmpArchivesDO> empArchivesDOList = selectDO(empArchivesQuery, siteId);
        if(CollectionUtils.isEmpty(empArchivesDOList)) {
            return empArchivesDOList == null ? null : new ArrayList<>();
        }
        return empArchivesDOList.stream()
                .map(empArchivesDO -> empArchivesDO.to(EmpArchivesListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpArchivesQuery empArchivesQuery, String siteId) {
        Example example = buildExample(empArchivesQuery,siteId);
        return empArchivesDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpArchivesListVO> selectPage(EmpArchivesQuery empArchivesQuery, String siteId) {
        Example example = buildExample(empArchivesQuery,siteId);
        Page<EmpArchivesDO> page = empArchivesDAO.selectPageByExample(example,
                empArchivesQuery.getPageNo(),
                empArchivesQuery.getPageSize());

        return page.to(EmpArchivesListVO.class);
    }

    @Override
    public EmpArchivesDO selectDOById(String empArchivesId, String siteId) {
        return listById(empArchivesId, siteId);
    }

    @Override
    public List<EmpArchivesDO> selectDO(EmpArchivesQuery empArchivesQuery, String siteId) {
        Example example = buildExample(empArchivesQuery,siteId);
        return empArchivesDAO.selectByExample(example);
    }

    @Override
    public List<EmpArchivesDO> listByEmpId(String empId, String siteId) {
        Example example = new Example(EmpArchivesDO.class);
        example.and()
                .andEqualTo("empId", empId)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return empArchivesDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void deleteByCompanyId(String companyId, TokenUser tokenUser) {
        Example example = new Example(EmpArchivesDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId());
        // 逻辑删除
        List<EmpArchivesDO> list = empArchivesDAO.selectByExample(example);
        String[] ids = list.stream()
                .map(BaseDO::getId).toArray(String[]::new);
        deleteByIds(tokenUser, ids);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void publishEmpArchivesList(List<EmpArchivesDTO> empArchivesList, String companyId, String empId, TokenUser tokenUser) {
        // 删除某个员工所有的
        deleteByEmpId(empId, tokenUser);

        if (CollectionUtils.isEmpty(empArchivesList)) {
            return;
        }

        empArchivesList.forEach(
                empArchivesDTO -> {
                    empArchivesDTO.setCompanyId(companyId);
                    empArchivesDTO.setEmpId(empId);
                    // 新增
                    EmpArchivesAddDTO empArchivesAddDTO = empArchivesDTO.to(EmpArchivesAddDTO.class);
                    save(empArchivesAddDTO, tokenUser);
                }
        );
    }

    /**
     * 删除某个员工所有的
     *
     * @param empId     员工id
     * @param tokenUser 当前用户
     */
    private void deleteByEmpId(String empId, TokenUser tokenUser) {
        // 逻辑删除
        List<EmpArchivesDO> list = listByEmpId(empId, tokenUser.getSiteId());
        String[] ids = list.stream()
                .map(BaseDO::getId).toArray(String[]::new);
        deleteByIds(tokenUser, ids);
    }


    /**
     * 根据查询参数，构建example

     * @param empArchivesQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpArchivesQuery empArchivesQuery, String siteId) {
        Example example = new Example(EmpArchivesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empArchivesQuery.getEmpId() != null) {
            example.and().andEqualTo("empId", empArchivesQuery.getEmpId());
        }
        if (empArchivesQuery.getArchivesNumber() != null) {
            example.and().andEqualTo("archivesNumber", empArchivesQuery.getArchivesNumber());
        }
        if (empArchivesQuery.getArchivesRemark() != null) {
            example.and().andEqualTo("archivesRemark", empArchivesQuery.getArchivesRemark());
        }
        if (empArchivesQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empArchivesQuery.getCompanyId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empArchivesQuery.getOrders());
        return example;
    }
}




